package com.ojoin.trade.common.web.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import com.ojoin.trade.common.web.domain.*;
import com.ojoin.trade.common.web.service.impl.UserUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.ojoin.trade.common.web.service.ButtonService;
import com.ojoin.trade.common.web.service.SysManageService;

 
@Controller
@RequestMapping(value = "sys/button")
public class ButtonController extends SimpleBaseController {

	@Autowired
	private SysManageService systemService;
	
	@Autowired
	private ButtonService buttonService;
	
	@ModelAttribute("button")
	public Button get(@RequestParam(required=false) Integer id) {
		if (null!=id){
			return systemService.getButton(id);
		}else{
			return new Button();
		}
	}

	@ModelAttribute("button")
    @ResponseBody
    @RequestMapping(value = "querybutton")
	public Result queryButtonListByMenuIdAndUserId(Integer menuId) {
        User user=UserUtils.getUser();
		List<Button> list = buttonService.queryButtonListByMenuIdAndUserId(menuId, user.getId());
		return new Result(Result.Status.OK, list);
	}
	
	@RequiresPermissions("user")
	@ResponseBody
	@RequestMapping(value ="list")
	public Result list() {
		List<Button> list = Lists.newArrayList();
		List<Button> sourcelist = systemService.findAllButton();
        return new Result(Result.Status.OK,list);
	}

	@ResponseBody
	@RequestMapping(value = "save")
	public Result save(Button button, Model model, RedirectAttributes redirectAttributes) {
		if(!systemService.getUser().isAdmin()){
			return new Result(Result.Status.OK, "越权操作，只有超级管理员才能添加或修改数据！");
		}
	 
		systemService.saveButton(button);
		return new Result(Result.Status.OK, "保存按钮'" + button.getName() + "'成功");
	}
	
	@RequiresPermissions("sys:button:edit")
	@ResponseBody
	@RequestMapping(value = "delete")
	public Result delete(Button button) {
			button.setDelFlag(BaseEntity.DEL_FLAG_DELETE);	
			systemService.deleteButton(button);
			return new Result(Result.Status.OK, "删除按钮成功");
	}

}
